package com.ihisun.tmhcms.core.service.userservice.impl;

import com.ihisun.tmhcms.core.dao.userdao.PreRevenueDao;
import com.ihisun.tmhcms.core.dao.userdao.UserAddressDao;
import com.ihisun.tmhcms.core.dao.userdao.UserDao;
import com.ihisun.tmhcms.core.dao.userdao.VipLevelTotalDao;
import com.ihisun.tmhcms.core.dto.userdto.PreRevenueDto;
import com.ihisun.tmhcms.core.dto.userdto.SearchUserDto;
import com.ihisun.tmhcms.core.dto.userdto.VipLevelTotalDto;
import com.ihisun.tmhcms.core.service.userservice.UserService;
import com.ihisun.tmhcms.core.vo.UserVo;
import com.ihisun.tmhcms.utils.DateUtils;
import com.ihisun.tmhcms.utils.PageBean;
import com.ihisun.tmhcms.utils.ResponseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.swing.text.html.parser.Entity;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    VipLevelTotalDao vipLevelTotalDao;
    @Resource
    EntityManager entityManager;
    @Resource
    UserDao userDao;
    @Resource
    UserAddressDao userAddressDao;
    @Resource
    PreRevenueDao preRevenueDao;

    @Override
    public ResponseVo countByVipNum() {


        List<VipLevelTotalDto> list = vipLevelTotalDao.vipHarmony();

        List<VipLevelTotalDto> harmony = vipLevelTotalDao.harmony();
        for (int i = 0; i < harmony.size(); i++) {
            VipLevelTotalDto vipLevelTotalDto = new VipLevelTotalDto();
            vipLevelTotalDto.setRole(0);
            vipLevelTotalDto.setHarmony(harmony.get(i).getHarmony());
            list.add(vipLevelTotalDto);
        }
        return ResponseVo.ok(list);
    }

    @Override
    public ResponseVo searchUser(SearchUserDto searchUserDto) {
        StringBuilder sql = new StringBuilder("select * from u_user where 1=1");
        if (searchUserDto.getUserName() != null && !"".equals(searchUserDto.getUserName())) {
            sql.append(" and nick_name=" + searchUserDto.getUserName() + "");
        }
        if (searchUserDto.getPhone() != null && !"".equals(searchUserDto.getPhone())) {
            sql.append(" and phone=" + searchUserDto.getPhone() + "");
        }
        if (searchUserDto.getRole() != null && !"".equals(searchUserDto.getRole())) {
            sql.append(" and role=" + searchUserDto.getRole() + "");
        }
        sql.append(" limit " + (searchUserDto.getPageNum() - 1) * 10 + "," + searchUserDto.getPageSize() + "");
        List<UserVo> resultList = entityManager.createNativeQuery(sql.toString(), UserVo.class).getResultList();
        for (int i = 0; i < resultList.size(); i++) {


            Integer integer = userDao.countByRecommenderId(resultList.get(i).getCustomerId());
            if (integer == null) {
                resultList.get(i).setCustomerNumber("0");
            } else {
                resultList.get(i).setCustomerNumber(integer.toString());
            }
            if (resultList.get(i).getOpeningMode().equals("1")) {
                resultList.get(i).setOpeningMode("好友邀请");
            } else {
                resultList.get(i).setOpeningMode("自行开通");
            }

            String role = resultList.get(i).getRole();
            // 1普通会员 2 pluss会员 3 特约经销商 4高级经销商5普通合伙人6高级合伙人
            if (role.equals("1")) {
                resultList.get(i).setRole("普通会员");
            }
            if (role.equals("2")) {
                resultList.get(i).setRole("plus会员");
            }
            if (role.equals("3")) {
                resultList.get(i).setRole("特约经销商");
            }
            if (role.equals("4")) {
                resultList.get(i).setRole("高级经销商");
            }
            if (role.equals("5")) {
                resultList.get(i).setRole("普通合伙人");
            }
            if (role.equals("6")) {
                resultList.get(i).setRole("高级合伙人");
            }

        }


        StringBuilder countSql = new StringBuilder("select count(*) from u_user where 1=1");
        if (searchUserDto.getUserName() != null && !"".equals(searchUserDto.getUserName())) {
            countSql.append(" and nick_name=" + searchUserDto.getUserName() + "");
        }
        if (searchUserDto.getPhone() != null && !"".equals(searchUserDto.getPhone())) {
            countSql.append(" and phone=" + searchUserDto.getPhone() + "");
        }
        if (searchUserDto.getRole() != null && !"".equals(searchUserDto.getRole())) {
            countSql.append(" and role=" + searchUserDto.getRole() + "");
        }
        BigInteger singleResult = (BigInteger) entityManager.createNativeQuery(countSql.toString()).getSingleResult();
        PageBean pageBean = new PageBean(searchUserDto.getPageNum(), singleResult.intValue(), searchUserDto.getPageSize(), resultList);
        return ResponseVo.ok(pageBean);
    }

    @Override
    public ResponseVo userInfo(String uuid) {


        Map<String, Object> map = new HashMap<>();
        UserVo byUuid = userDao.findByUuid(uuid);
        if (byUuid.getRecommenderId()!=null&&!"".equals(byUuid.getRecommenderId())){
            UserVo byCustomerId = userDao.findByCustomerId(byUuid.getRecommenderId());
            byUuid.setRecommenderName(byCustomerId.getNickName());
        }



        if (byUuid.getRole().equals("1")) {
            byUuid.setRole("普通会员");
        }
        if (byUuid.getRole().equals("2")) {
            byUuid.setRole("plus会员");
        }
        if (byUuid.getRole().equals("3")) {
            byUuid.setRole("特约经销商");
        }
        if (byUuid.getRole().equals("4")) {
            byUuid.setRole("高级经销商");
        }
        if (byUuid.getRole().equals("5")) {
            byUuid.setRole("普通合伙人");
        }
        if (byUuid.getRole().equals("6")) {
            byUuid.setRole("高级合伙人");
        }
        Integer integer = userDao.countByRecommenderId(byUuid.getCustomerId());
        if (integer == null) {
            byUuid.setCustomerNumber("0");
        } else {
            byUuid.setCustomerNumber(integer.toString());
        }
        Integer integer1 = userAddressDao.countByUuid(byUuid.getUuid());
        if (integer1 == null) {
            byUuid.setAddressNumber("0");
        } else {
            byUuid.setAddressNumber(integer1.toString());
        }

        Double byachievement = preRevenueDao.findByachievement(uuid);
        if (byachievement == null) {
            byUuid.setAchievement("0.0");
        } else {
            byUuid.setAchievement(byachievement.toString());
        }
        Double byrecommendUuid = preRevenueDao.findByrecommendUuid(uuid);
        if (byrecommendUuid == null) {
            byUuid.setProfit("0.0");
        } else {
            byUuid.setProfit(byrecommendUuid.toString());
        }
        map.put("userInfo", byUuid);

        List<UserVo> byRecommenderId = userDao.findByRecommenderId(byUuid.getCustomerId());
        if (byRecommenderId != null && !byRecommenderId.isEmpty()) {

            for (int i = 0; i < byRecommenderId.size(); i++) {

                if (byRecommenderId.get(i).getRole().equals("1")) {
                    byRecommenderId.get(i).setRole("普通会员");
                }
                if (byRecommenderId.get(i).getRole().equals("2")) {
                    byRecommenderId.get(i).setRole("plus会员");
                }
                if (byRecommenderId.get(i).getRole().equals("3")) {
                    byRecommenderId.get(i).setRole("特约经销商");
                }
                if (byRecommenderId.get(i).getRole().equals("4")) {
                    byRecommenderId.get(i).setRole("高级经销商");
                }
                if (byRecommenderId.get(i).getRole().equals("5")) {
                    byRecommenderId.get(i).setRole("普通合伙人");
                }
                if (byRecommenderId.get(i).getRole().equals("6")) {
                    byRecommenderId.get(i).setRole("高级合伙人");
                }
            }
        }
        map.put("customer", byRecommenderId);

        List<PreRevenueDto> byRecommendUuidAndOrderState = preRevenueDao.findByRecommendUuidAndOrderState(uuid, "3");
        for (int i = 0; i < byRecommendUuidAndOrderState.size(); i++) {
            if (byRecommendUuidAndOrderState.get(i).getConsumerUuid() != null) {
                UserVo byUuid1 = userDao.findByUuid(byRecommendUuidAndOrderState.get(i).getConsumerUuid());
                if (byUuid1.getNickName() != null) {
                    byRecommendUuidAndOrderState.get(i).setUserName(byUuid1.getNickName());
                }

            }
        }
        map.put("profit", byRecommendUuidAndOrderState);
        return ResponseVo.ok(map);
    }

    @Override
    public ResponseVo updateRole(String uuid, String role) {
        UserVo byUuid = userDao.findByUuid(uuid);
        byUuid.setUpdateTime(DateUtils.getCurrentTime());
        byUuid.setOpeningDate(DateUtils.getCurrentTime());
        byUuid.setRole(role);
        userDao.save(byUuid);
        return ResponseVo.ok(byUuid);
    }
}
